package com.evangelion.core.daoImpl;


import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.context.FacesContext;
import javax.naming.InitialContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.ServletContext;
import javax.sql.DataSource;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Kellycasma
 */
public class ConnectionUtil extends GenericDaoImpl<Serializable> implements Serializable {

    private static ConnectionUtil INSTANCE = new ConnectionUtil();
    private static Connection connection;

    private ConnectionUtil() {
    }

    private void init() {
        if (INSTANCE == null) {
            INSTANCE = new ConnectionUtil();
        }
    }

    private static boolean isConect() throws SQLException {
        if (connection == null || connection.isClosed()) {
            return false;
        }
        return true;
    }

    public static boolean connect() {
        try {
            if (!isConect()) {
                try {
                    InitialContext initialContext = new InitialContext();
                    DataSource dataSource = (DataSource) initialContext.lookup("JNDUEVANGELION");
                    connection = dataSource.getConnection();
    // connection = em.unwrap(java.sql.Connection.class);

                    return true;
                } catch (Exception ex) {
                    System.out.println(ex.getMessage());
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(ConnectionUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    @Transactional
    public boolean connectWrap() {
        try {
            if (!isConect()) {
                try {
    // InitialContext initialContext = new InitialContext();
    // DataSource dataSource = (DataSource) initialContext.lookup("jdbc/amsys");
    // connection = dataSource.getConnection();
                    System.out.println("EntityManager: " + em);
                    connection = em.unwrap(java.sql.Connection.class);

                    return true;
                } catch (Exception ex) {
                    System.out.println(ex.getMessage());
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(ConnectionUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    public static boolean closeConect() {
        try {
            if (isConect()) {
                try {
                    connection.close();
                    return true;
                } catch (Exception ex) {
                    System.out.println(ex.getMessage());
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(ConnectionUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }

    public synchronized static Connection getConnection() {
        connect();
        return connection;
    }

    public static ConnectionUtil getInstance() {
        return INSTANCE;
    }

    /**
     *
     * @return
     */
    public static ServletContext getServletContext() {
        return (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
    }
}